package countSubstringsThatSatisfyKConstraintI_3258;

//https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/
public class Solution {
    public int countKConstraintSubstrings(String s, int k) {
        int[] count = new int[2];
        int left = 0;
        int result = 0;
        for (int r = 0; r < s.length(); r++) {
            char c = s.charAt(r);
            if (c == '1') {
                count[1] += 1;
            } else {
                count[0] += 1;
            }
            while (count[1] > k && count[0] > k) {
                if (s.charAt(left++) == '1') {
                    count[1] += 1;
                } else {
                    count[0] += 1;
                }
            }
            result += r - left + 1;
        }
        return result;
    }
}
